﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WebeTerm.PlugIns;
using WebeTerm.Common;
using System.Net;
using System.Text.RegularExpressions;

namespace WebeTerm.GetRebate
{
    /// <summary>
    /// 航空公司返扣信息获取插件基类
    /// </summary>
    public abstract class BaseRebatePlugIn : BaseTermSequencePlugIn<RebateResult, RebateConfig>
    {
        CookieContainer cookie;
        HttpHeader header = new HttpHeader();
        public virtual void GetVeryCode() {
            base.ReadJSonConfig();
            header = new HttpHeader();
            header.accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/x-silverlight, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, application/x-silverlight-2-b1, */*";
            header.contentType = "application/x-www-form-urlencoded";
            header.method = "POST";
            header.userAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)";
            header.maxTry = 300;
            string vCode = string.Empty;
            string Code = string.Empty;
            cookie = HttpHelper.GetCooKie(this.PlugInConfig.WebLogInUrl, string.Empty, header);
            if(!string.IsNullOrEmpty( this.PlugInConfig.VerifyCodeControl))
                HttpHelper.DowloadCheckImg(this.PlugInConfig.CodeUrl, cookie, string.Format(@"{0}.jpg",this.PlugInConfig.AirWayCode));

        }

        public virtual void LogIn(string code) {
            var result = HttpHelper.POST(this.PlugInConfig.WebLogInUrl,string.Format( @"{0}={3}&{1}={4}&{2}={5}",this.PlugInConfig.UserNameControl,this.PlugInConfig.PasswordControl,this.PlugInConfig.VerifyCodeControl,this.PlugInConfig.UserName,this.PlugInConfig.Password,code), cookie, header);
            Console.Write(result);
        }

        public override void Execute()
        {
            this.ReadJSonConfig();
            var html = HttpHelper.POST(this.PlugInConfig.RequestUrl+this.PlugInConfig.Code, string.Empty, cookie, header);
            UnpacketHtml(html);
        }

        protected virtual void UnpacketHtml(string html) {
            RebateResult result = new RebateResult() { 
             Code = this.PlugInConfig.Code,
              PNRCode = this.PlugInConfig.PnrNo,
            };
            var m =Regex.Matches(html, @"\<span\s+id\=\""span_fee_(\d)_\d\""\>\d+(\.\d{0,}){0,1}\((\d+(\.\d{0,}){0,1})\)", RegexOptions.IgnoreCase | RegexOptions.Multiline);
            m.Cast<Match>().OrderBy(l=>l.Groups[1].Value).ToList().ForEach(l => {
                result.Rebates.Add(l.Groups[3].Value);
            });
            FireOnUnPackResult(new eTermPlugInEventArgs(html,result));
        }

        protected override List<RebateResult> GetStreamResult(string Cmd, string Result)
        {
            List<RebateResult> items = new List<RebateResult>();
            
            return items;
        }
    }
}
